package org.nf.dao.impl;

import org.nf.dao.UserDao;
import org.nf.dbutil.DbUtil;
import org.nf.entity.User;
import org.nf.utils.SqlRunner;

import java.sql.Connection;
import java.util.List;


/**
 * @Author lzp
 * @Date 2024-04-25
 */
public class UserDaoImpl implements UserDao {
    @Override
    public User selectUser(String username) {

        // 定义sql语句
        String sql = "SELECT `id`,`username`,`password`," +
                "`level`,`image`,`phone`,`email`,`registerTime`,`status` FROM user where username = ?";

        //获取连接对象 实例化 SqlRunner 对象
        Connection conn = DbUtil.getConnection();
        SqlRunner sqlRunner = new SqlRunner(conn);

        //执行sql
        List<User> list = sqlRunner.query(User.class, sql, username);

        return list.isEmpty() ? null : list.getFirst();
    }

    public static void main(String[] args) {
        UserDao userDao = new UserDaoImpl();
        User s = userDao.selectUser("张三");
        System.out.println(s);
        if(s != null) {
            System.out.println(s.getId()+" "+s.getUsername());
        } else {
            System.out.println("null");
        }
    }
}
